package com.zeh.billing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zeh.billing.config.CommonResult;
import com.zeh.billing.entity.Admin;
import com.zeh.billing.entity.AdminDto;
import com.zeh.billing.entity.LoginDo;
import com.zeh.billing.entity.LoginToken;
import com.zeh.billing.mapper.AdminMapper;
import com.zeh.billing.service.AdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zeh
 * @since 2021-08-20
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public CommonResult login(LoginDo admin) {

        Admin admin1 = adminMapper.selectOne(
                new QueryWrapper<Admin>().lambda().eq(Admin::getUsername, admin.getUsername())
        );
        if (admin1 == null) {
            return CommonResult.failed("用户账户不存在！");
        }else {
            if (!(admin1.getPassword().equals(admin.getPassword()))) {
                return CommonResult.failed("用户密码不正确");
            }
        }
        LoginToken loginToken = new LoginToken();
        loginToken.setToken("admin");

        return CommonResult.success(loginToken);
    }


    @Override
    public CommonResult register(AdminDto admin) {
        Admin admin1 = new Admin();
        if (StringUtils.isEmpty(admin.getPassword())) {
            admin.setPassword("000000");
        }
        BeanUtils.copyProperties(admin, admin1);
        int insert = adminMapper.insert(admin1);
        if (insert == 1) {
            return CommonResult.success("注册成功！");
        } else {
            return CommonResult.failed("注册失败！");
        }
    }

}
